# Author: HZT
# Date: 2023/10/22
import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

import page
from config.config_loader import config
from page.web_ck_lj import WebCkLj
from utils import UtilsDriver, login_with_session, GetLog, generate_random_string


class TestWebCkLj:
    wz = ""

    def setup_class(self):
        self.log = GetLog.get_log()
        self.driver = UtilsDriver.get_web_driver()
        # 实例化
        self.ckLj = WebCkLj(self.driver)
        # 打开网页
        self.driver.get(config.URL)
        login_with_session('system1', self.driver)
        self.km = generate_random_string(15)
        self.me = '50'
        self.bz = '学习雷锋好榜样'

    def teardown_class(self):
        # 在所有测试用例执行完毕后，关闭浏览器
        # UtilsDriver.get_web_driver().quit()
        pass

    # 新增卡图链接
    def test01_ck_lj(self):
        try:
            self.ckLj.ck_lj_click_ktlj()
            self.ckLj.ck_lj_get_ktljym()
            self.ckLj.ck_lj_click_xz()
            self.ckLj.ck_lj_xz(self.driver, km=self.km, me=self.me, bz=self.bz, filename="test002.png")
            self.ckLj.ck_lj_get_xz()
            km = self.ckLj.ck_lj_get_kmlb()
            me = self.ckLj.ck_lj_get_melb()
            TestWebCkLj.wz = self.ckLj.ck_lj_get_wzlb()
            self.log.info(TestWebCkLj.wz)
            bz = self.ckLj.ck_lj_get_bzlb()
            # 判断新增数据是否正确
            assert km == self.km
            assert me == self.me
            assert bz == self.bz
        except Exception as e:
            self.log.error(f"新增图片管理lianjie异常，原因是{e}")
            self.ckLj.base_get_img()
            assert False

    def test02_ck_lj(self):
        try:
            driver_new = webdriver.Chrome()
            driver_new.maximize_window()
            driver_new.get(config.URL)
            # 登录
            login_with_session('system2', driver_new)
            WebDriverWait(self.driver, config.TIME_OUT, config.POLL).until(
                EC.presence_of_all_elements_located((By.XPATH, "//span[contains(text(),'总览')]")))
            self.log.info(TestWebCkLj.wz)
            driver_new.get(TestWebCkLj.wz)
            self.ckLj = WebCkLj(driver_new)
            self.ckLj.ck_lj_get_ckktlj()
            assert self.ckLj.ck_lj_get_ckkm(self.km) is not None
            assert self.ckLj.ck_lj_get_ckme(self.me) is not None
        except Exception as e:
            self.log.error(f"查看卡密链接异常，原因是{e}")
            self.ckLj.base_get_img()
            assert False
